package com.fish.yssb.ui.page.fragment;

import android.os.Bundle;
import android.os.Handler;
import android.support.annotation.Nullable;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ScrollView;
import android.widget.TextView;

import com.fish.yssb.Consts;
import com.fish.yssb.R;
import com.fish.yssb.app.BaseApp;
import com.fish.yssb.data.model.LoginUser;
import com.fish.yssb.data.model.TopRK;
import com.fish.yssb.ui.page.activity.AccountLoginActivity;
import com.fish.yssb.ui.page.activity.setPara.SetParaListActivity;
import com.fish.yssb.ui.page.adapter.AppAdapter;
import com.fish.yssb.ui.page.widget.HorizontalPageLayoutManager;
import com.fish.yssb.ui.page.widget.PagingItemDecoration;
import com.fish.yssb.ui.page.widget.PagingScrollHelper;

import java.util.ArrayList;
import java.util.List;


/**
 * Created by caobotao on 16/1/4.
 */
public class LSFragment extends BaseFragment implements View.OnClickListener, PagingScrollHelper.onPageChangeListener {

    private ViewGroup mMoreLayout;
    private List<ImageView> views = new ArrayList<ImageView>();

    private static boolean frist = true;
    private View mView;
    private LinearLayout  ll_top1,ll_top2,ll_top3,ll_top4;
    private ImageView im_icon1,im_icon2,im_icon3,im_icon4;
    private ScrollView sv_list;

//    private String[] imageUrls = {};

    private ArrayList<String> imageUrls = new ArrayList<String>();
    private ArrayList<String> showUrls = new ArrayList<String>();
    private LinearLayout noticeLayout;
    private TextView tv_name,tv_login;
    private ImageView closeNotice;

    //首页顶部应用
    AppAdapter topAppAdapter;
    RecyclerView recyclerView;
    PagingScrollHelper scrollHelper = new PagingScrollHelper();

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        if (mView == null) {
            mView = inflater.inflate(R.layout.fragment_tab_ls, container, false);

        }
//        initIconNew(mView);
//        ll_top1 = mView.findViewById(R.id.ll_top1);
//        ll_top2 = mView.findViewById(R.id.ll_top2);
//        ll_top3 = mView.findViewById(R.id.ll_top3);
//        ll_top4 = mView.findViewById(R.id.ll_top4);
//
//        im_icon1 = mView.findViewById(R.id.im_icon1);
//        im_icon2 = mView.findViewById(R.id.im_icon2);
//        im_icon3 = mView.findViewById(R.id.im_icon3);
//        im_icon4 = mView.findViewById(R.id.im_icon4);
//        tv_login = mView.findViewById(R.id.tv_login);
//        tv_name  = mView.findViewById(R.id.tv_name);
//        sv_list = mView.findViewById(R.id.sv_list);
//
//
//
//
//        new Handler().postDelayed(new Runnable() {
//            @Override
//            public void run() {
//                sv_list.scrollTo(0,0);
//            }
//        }, 100);
//
//
//
//        im_icon1.setOnClickListener(new View.OnClickListener() {
//            @Override
//            public void onClick(View v) {
//                LoginUser user = BaseApp.getInstance().user;
//                if(!"".equalsIgnoreCase(user.getUserName())) {
//                    SetParaListActivity.start(getActivity());
//                }else{
////                    AccountLoginActivity.start(getActivity());
//                }
//            }
//        });
//        tv_login.setOnClickListener(new View.OnClickListener() {
//            @Override
//            public void onClick(View v) {
//                LoginUser user = BaseApp.getInstance().user;
//                if("".equalsIgnoreCase(user.getUserName()))
//                {
//                    AccountLoginActivity.start(getActivity());
//                }else{
//                    user.setUserName("");
//                    BaseApp.getInstance().saveUser();
//                    rePage();
//                }
//            }
//        });
        return mView;
    }

    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);


    }


    @Override
    public void onDestroy() {
        super.onDestroy();
        //在界面销毁的地方要解绑

    }


    @Override
    public void onResume() {
        super.onResume();
    }




    @Override
    public void onClick(View v) {

    }

    private PagingItemDecoration pagingItemDecoration = null;
    private HorizontalPageLayoutManager horizontalPageLayoutManager = null;
    public void initIconNew(View mView)
    {

        horizontalPageLayoutManager = new HorizontalPageLayoutManager(3, 4);
        pagingItemDecoration = new PagingItemDecoration(getActivity(), horizontalPageLayoutManager);

        recyclerView = (RecyclerView) mView.findViewById(R.id.recyclerview);
        ArrayList topAppData = new ArrayList<TopRK>();

        topAppAdapter = new AppAdapter(getActivity(),topAppData);
        recyclerView.setAdapter(topAppAdapter);
        scrollHelper.setUpRecycleView(recyclerView);
        scrollHelper.setOnPageChangeListener(this);
        recyclerView.setHorizontalScrollBarEnabled(true);
        RecyclerView.LayoutManager layoutManager = null;
        RecyclerView.ItemDecoration itemDecoration = null;

        layoutManager = horizontalPageLayoutManager;
//        itemDecoration = pagingItemDecoration;

        if (layoutManager != null) {
            recyclerView.setLayoutManager(layoutManager);
//            recyclerView.removeItemDecoration(lastItemDecoration);
//            recyclerView.addItemDecoration(itemDecoration);
            scrollHelper.updateLayoutManger();
            scrollHelper.scrollToPosition(0);

//            lastItemDecoration = itemDecoration;
        }

        TopRK top1 = new TopRK();
        top1.setTitle(Consts.toprk1);
        topAppData.add(top1);

        TopRK top2 = new TopRK();
        top2.setTitle(Consts.toprk2);
        topAppData.add(top2);

        TopRK top3 = new TopRK();
        top3.setTitle(Consts.toprk3);
        topAppData.add(top3);

        TopRK top4 = new TopRK();
        top4.setTitle(Consts.toprk4);
        topAppData.add(top4);

        TopRK top5 = new TopRK();
        top5.setTitle(Consts.toprk5);
        topAppData.add(top5);

        TopRK top6 = new TopRK();
        top6.setTitle(Consts.toprk6);
        topAppData.add(top6);

        TopRK top7 = new TopRK();
        top7.setTitle(Consts.toprk7);
        topAppData.add(top7);

        TopRK top8 = new TopRK();
        top8.setTitle(Consts.toprk8);
        topAppData.add(top8);

        TopRK top9 = new TopRK();
        top9.setTitle(Consts.toprk9);
        topAppData.add(top9);

        TopRK top10 = new TopRK();
        top10.setTitle(Consts.toprk10);
        topAppData.add(top10);

        TopRK top11 = new TopRK();
        top11.setTitle(Consts.toprk11);
        topAppData.add(top11);
//        topAppAdapter.updateData(topAppData);





//        updateData();
    }

    @Override
    public void onPageChange(int index) {

    }
}
